package at.jku.se.fastsell.gui;

import at.jku.se.fastsell.data.Auction;

/**
 * Filter class for auction table
 */
public class AuctionsTableFilter {

	private boolean ended;
	private String onlyUser, articleDescription;

	// ----------------------------------------------------------------------------------

	/**
	 * Creates a filter for auction table
	 * 
	 * @param ended
	 *            - true to filter all active auctions
	 * @param onlyUser
	 *            - set user to show only auctions with given user as highest
	 *            bidder or buyer
	 */
	public AuctionsTableFilter(boolean ended, String onlyUser) {
		this.ended = ended;
		this.onlyUser = onlyUser;
	}

	public AuctionsTableFilter(String articleDescription) {
		this.articleDescription = articleDescription;
	}

	// ----------------------------------------------------------------------------------

	/**
	 * Filters auction object according to given parameters
	 * 
	 * @param a
	 *            - Auction object
	 * @return - True if auction has to be filtered
	 * 
	 */
	public boolean filterAuction(Auction a) {
		if (articleDescription != null) {
			if (a.getDescription().toLowerCase().contains(articleDescription.toLowerCase()) || a.getTitle().toLowerCase().contains(articleDescription.toLowerCase())) {
				return false;
			}
		} else {
			if (!a.isActive() == ended && a.hasBids()
					&& a.getHighestBidder().getName().equals(onlyUser)) {
				return false;
			}
		}
		return true;
	}

}
